﻿using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;
using wnet.admin.Models;
using System;
using Models;
using wnet.admin.Models.dto;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;




namespace wnet.admin.Controllers.ywBusiness
{
    [Route("ywbusiness/chart/{action}")]
    public class ChartController : ApiController
    {

        /// <summary>
        /// 分析图设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object setLineConfig(List<bool> dlist)
        {
            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist
            };
            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 分析图设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public object setLineConfigv2(setupdto entity)
        {
            var data = new
            {
                code = 200,
                msg = "成功",
                data = entity
            };
            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 清屏
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object setClearscreen()
        {
            var data = new
            {
                code = 200,
                msg = "清屏成功",
                data = ""
            };
            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }




        /// <summary>
        ///  chart 线集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetLineData2()
        {
            string physicalPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
            string contents = System.IO.File.ReadAllText($"{physicalPath}/DataBase/chartLineData.txt");
            var data = new
            {
                code = 200,
                msg = "成功",
                data = JsonConvert.DeserializeObject<DataModel>(contents)
            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// chart 线集合
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpGet]
        public object GetLineData()
        {
            List<string> Days = new List<string>();
            try
            {
                var AllData = ConstDefine.AllData;
                //日期，X轴底部展示
                for (int i = 1; i < ConstDefine.Project.SenSorNum * ConstDefine.Project.BoardNum + 1; i++)
                {
                    Days.Add(i.ToString());
                }

                var data = DealData(AllData);
                int rows = data.Count; // 外层数组的大小
                DataModel dataModel = new DataModel();
                int[][] waveData = new int[rows][]; // 声明二维数组
                //var categories = new[] { "Category 1", "Category 2", "Category 3" };
                dataModel.categories = Days.ToArray();
                dataModel.seriesData = waveData;
                dataModel.Locations = new int[data[0].Count];
                for (int i = 0; i < data[0].Count; i++)
                {
                    dataModel.Locations[i] = data[0][i].X;
                }


                for (int i = 0; i < rows; i++)
                {
                    int cols = data[i].Count; // 内层数组的大小
                    waveData[i] = new int[cols]; // 初始化内层数组
                    for (int j = 0; j < cols; j++)
                    {
                        waveData[i][j] = data[i][j].Y;
                    }
                }
                var resdata = new
                {
                    code = 200,
                    msg = "成功",
                    data = dataModel
                };

                return Json(resdata, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

            }
            catch (Exception ex)
            {
                return null;
            }
        }


        private List<List<Point>> DealData(object obj)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(stream, obj);
                stream.Seek(0, SeekOrigin.Begin);

                //反序列化
                List<List<Point>> res = (List<List<Point>>)bf.Deserialize(stream);

                return res;
            }
        }

        public class DataModel
        {
            public string[] categories { get; set; }
            public int[][] seriesData { get; set; }

            public int[] Locations { get; set; }
        }
    }
}